package com.api.pay.controller;

import cn.hutool.core.bean.BeanUtil;
import com.api.apicommon.response.BaseResponse;
import com.api.apicommon.response.ResultUtils;
import com.api.pay.model.domain.ApiGoods;
import com.api.pay.model.dto.goods.GoodCreateRequest;
import com.api.pay.model.dto.goods.GoodQueryRequest;
import com.api.pay.service.ApiGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private ApiGoodsService apiGoodsService;

    /**
     * 分页查询商品
     *
     * @return
     */
    @GetMapping("/query/list")
    public BaseResponse queryGoodsList(GoodQueryRequest goodQueryRequest) {
        QueryWrapper<ApiGoods> goodsQueryWrapper = new QueryWrapper<>();
        long current = goodQueryRequest.getCurrent();
        long pageSize = goodQueryRequest.getPageSize();
        Page<ApiGoods> apiGoodsPage = apiGoodsService.page(new Page<>(current, pageSize), goodsQueryWrapper);
        return ResultUtils.success(apiGoodsPage);
    }

    /**
     * 创建商品
     * @param goodCreateRequest
     * @return
     */
    @PostMapping("/create")
    public BaseResponse createGoods(@RequestBody GoodCreateRequest goodCreateRequest){
        ApiGoods apiGoods = new ApiGoods();
        BeanUtil.copyProperties(goodCreateRequest,apiGoods);
        boolean save = apiGoodsService.save(apiGoods);
        return ResultUtils.success(save);
    }

}
